package java学习.实训.day06.program;

/**
 * **************
 * 项目名称: 蓝桥杯 <br/>
 * 文件名称:  <br/>
 * 文件描述: 这里添加您的类文件描述，说明当前文件要包含的功能。 <br/>
 * 文件创建：刘世锦 <br/>
 * 创建时间: 2022/6/14 <br/>
 *
 * @version v1.0 <br/>
 * @update [序号][日期YYYY-MM-DD][更改人姓名][变更描述]<br/>
 * *************
 */
public class Rule {
//    public  Chessboard.pieceArr;
    private int row  = Chessboard.ROW   ;
    private int colum = Chessboard.COLUMN;
    //判断是否胜利
    public boolean checkWin(Piece piece) {
        Piece[][] pieceArr = Chessboard.pieceArr;
        int x = piece.getX();
        int y = piece.getY();
        
        boolean flag = false;
        //判断横向是否有五个棋子是相同的颜色
        int count1 = 1;//相同颜色棋子的个数
        String color = null;
        if (pieceArr[x][y].getSymbol().equals(Main.WHITE) || pieceArr[x][y].getSymbol().equals(Main.BLACK)){
            color = pieceArr[x][y].getSymbol(); //刚下的棋子的颜色
        }
        int count = 1;  //棋子索引的增量
        while((x+count)<row && color.equals(pieceArr[x + count][y].getSymbol())){
            count1++;
            count++;
        }
        count = 1;
        while((x-count)>=0 && color.equals(pieceArr[x - count][y].getSymbol())){
            count1++;
            count++;
        }
        if(count1 >= 5){
            flag = true;
        }

        //判断纵向是否有五个棋子是相同的颜色
        int count2 = 1;
        count = 1;
        while((y+count)<colum && color.equals(pieceArr[x][y + count].getSymbol())){
            count2++;
            count++;
        }
        count = 1;
        while((y-count)>=0 && color.equals(pieceArr[x][y - count].getSymbol())){
            count2++;
            count++;
        }
        if(count2 >= 5){
            flag = true;
        }

        //右上    左下 是否有五个棋子是相同的颜色
        int count3 = 1;
        count = 1;
        while((x+count)<row && (y-count)>=0 && color.equals(pieceArr[x + count][y - count].getSymbol())){
            count3++;
            count++;
        }
        count = 1;
        while((x-count)>=0 && (y+count)<colum && color.equals(pieceArr[x - count][y + count].getSymbol())){
            count3++;
            count++;
        }
        if(count3 >= 5){
            flag = true;
        }

        //左上  右下  是否有五个棋子是相同的颜色
        int count4 = 1;
        count = 1;
        while((x-count)>0 && (y-count)>=0 && color.equals(pieceArr[x - count][y - count].getSymbol())){
            count4++;
            count++;
        }
        count = 1;
        while((x+count)<row && (y+count)<colum && color.equals(pieceArr[x + count][y + count].getSymbol())){
            count4++;
            count++;
        }
        if(count4 >= 5){
            flag = true;
        }
        return flag;
    }
}
